﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MBSDAL;

namespace MBSBusinessEntities
{
    public class MovieBE : ICrud
    {

        public MovieBE()
       {
         mbs = new MBS_Entities();
       }

        public MovieBE(string MovieTitle, string Description, int Duration, string Genre, string Cast, float Rating, string ImageURL, DateTime ReleaseDate)
       {
          
           this.MovieTitle = MovieTitle;
           this.Description = Description;
           this.Duration = Duration;
           this.Genre=Genre;
           this.Cast=Cast;
           this.Rating= Rating;
           this.ImageURL=ImageURL;
           this.ReleaseDate=ReleaseDate;
           this.TrailerURL = TrailerURL;
           mbs = new MBS_Entities();
       }

        #region ClassVariables

        private MBS_Entities mbs;

        private int movieID;

        private string movieTitle;

        private string description;

        private int duration;

        private string genre;

        private string cast;

        private float rating;

        private string imageURL;

        private DateTime releaseDate;

        private bool isDeleted;

        private string trailerURL;

        public string TrailerURL
        {
            get { return trailerURL; }
            set { trailerURL = value; }
        }
        
        #endregion


        #region Properties


        public int MovieID
        {
            get { return movieID; }
            set { movieID = value; }
        }
        public string MovieTitle
        {
            get { return movieTitle; }
            set { movieTitle = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public int Duration
        {
            get { return duration; }
            set { duration = value; }
        }

        public string Genre
        {
            get { return genre; }
            set { genre = value; }
        }


        public string Cast
        {
            get { return cast; }
            set { cast = value; }
        }

        public float Rating
        {
            get { return rating; }
            set { rating = value; }
        }

        public string ImageURL
        {
            get { return imageURL; }
            set { imageURL = value; }
        }

        public DateTime ReleaseDate
        {
            get { return releaseDate; }
            set { releaseDate = value; }
        }

        public bool IsDeleted
        {
            get { return isDeleted; }
            set { isDeleted = value; }
        }

        #endregion


        #region Methods

        #endregion


        public bool Add()
        {
            Movie movie = new Movie();
            try
            {

                 movie.MovieTitle=this.MovieTitle;
                 movie.Description=this.Description;
                 movie.Duration= this.Duration  ;
                 movie.Genre= this.Genre ;
                 movie.Cast= this.Cast  ;
                 movie.Rating=this.Rating  ;
                 movie.ImageURL=this.ImageURL  ;
                 movie.ReleaseDate= this.ReleaseDate ;
                 movie.IsDeleted = false;
                 movie.TrailerUrl = this.TrailerURL;

                 mbs.AddToMovies(movie);
                 mbs.SaveChanges();
                
                
            }
            catch (System.Data.UpdateException ex)
            {
                //Duplicate moviename exists in the database.
                return false;
            }

            return true; 
        }

        public bool Delete(int MovieID)
        {
            Movie movie = GetMovieDALByID(MovieID);

            if (movie == null)
            {
                return false;
            }

            movie.IsDeleted = true;
            mbs.SaveChanges();

            MovieScheduleBE movieScheduleBE = new MovieScheduleBE();
            movieScheduleBE.DeleteMovieScheduleOfSpecificMovie(movie.MovieID);

            return true;
        }

        public bool Update()
        {
            Movie movie = GetMovieDALByID(MovieID);
            movie.MovieTitle = this.MovieTitle;
            movie.Description = this.Description;
            movie.Duration = this.Duration;
            movie.Genre = this.Genre;
            movie.Cast = this.Cast;
            movie.Rating = this.Rating;
            movie.ImageURL = this.ImageURL;
            movie.ReleaseDate = this.ReleaseDate;
            movie.TrailerUrl = this.TrailerURL;


            mbs.SaveChanges();
            return true;


        }

        public object GetbyID(int MovieID)
        {

            MovieBE moviebe = new MovieBE();
            Movie movie = GetMovieDALByID(MovieID);
           
            moviebe = ConvertDALToBusinessEntity(movie);
            return moviebe;

        }

        public MovieBE GetMovieByID(int MovieID)
        {
            MovieBE moviebe = new MovieBE();
            Movie movie = GetMovieDALByID(MovieID);

            moviebe = ConvertDALToBusinessEntity(movie);
            return moviebe;
        }

        private MovieBE ConvertDALToBusinessEntity(Movie movieDAL)
        {
            if (movieDAL == null) return null;
            MovieBE moviebe = new MovieBE();

            moviebe.MovieID = movieDAL.MovieID;
            moviebe.MovieTitle = movieDAL.MovieTitle;
            moviebe.Description = movieDAL.Description;
            moviebe.Duration = Convert.ToInt32(movieDAL.Duration);
            moviebe.Genre = movieDAL.Genre;
            moviebe.Cast = movieDAL.Cast;
            moviebe.Rating = (float)movieDAL.Rating;
            moviebe.ImageURL = movieDAL.ImageURL;
            moviebe.ReleaseDate = Convert.ToDateTime(movieDAL.ReleaseDate);
            moviebe.IsDeleted = movieDAL.IsDeleted;
            moviebe.trailerURL = movieDAL.TrailerUrl;

            return moviebe;
        }

        private List<Movie> GetAllDALMovies()
        {
            return mbs.Movies.Where(x=>x.IsDeleted==false).ToList();
        }

        public List<MovieBE> GetAllMovies()
        {

            List<Movie> dalmovielist = GetAllDALMovies();
            List<MovieBE> movielist = new List<MovieBE>();

            foreach (Movie dalmovie in dalmovielist)
            {
                MovieBE moviebe = ConvertDALToBusinessEntity(dalmovie);
                movielist.Add(moviebe);
            }

            return movielist;

        }

        private Movie GetMovieDALByID(int MovieID)
        {
            Movie movieDAL = new Movie();
            try
            {
                movieDAL = mbs.Movies.First(x => x.MovieID == MovieID);
            }
            catch (System.InvalidOperationException ex)
            {
                return null;
            }

            return movieDAL;

        }

        

        public int GetMovieIDByImageUrl(string ImageURL)
        {
            Movie movieDAL = new Movie();
            try
            {
                movieDAL = mbs.Movies.First(x => x.ImageURL == ImageURL);
            }
            catch (System.InvalidOperationException ex)
            {
                return 0;
            }

            return movieDAL.MovieID;

        }

        private Movie GetMovieDALByName(string MovieTitle)
        {
            Movie movieDAL = new Movie();
            try
            {
                movieDAL = mbs.Movies.First(x => x.MovieTitle.Equals(MovieTitle));
            }
            catch (System.InvalidOperationException ex)
            {
                return null;
            }

            return movieDAL;

        }
        public int GetIDbyName(string MovieTitle)
        {
            MovieBE moviebe = new MovieBE();
            Movie movie = GetMovieDALByName(MovieTitle);
            moviebe = ConvertDALToBusinessEntity(movie);
            return moviebe.MovieID;
        }



        public object GetByID(int id)
        {
            throw new NotImplementedException();
        }


        private List<Movie> GetAllDALMoviesBasedOnCinemaToDisplay(int CinemaID)
        {

            List<Movie> movieList = new List<Movie>();
            DateTime StartQueryDate = DateTime.Now;
            DateTime EndQueryDate = StartQueryDate.AddDays(5);
            List<ShowTime> showTimeList = mbs.ShowTimes.Where(s => s.MovieSchedule.CinemaID == CinemaID && s.ShowDateTime >= StartQueryDate && s.ShowDateTime <= EndQueryDate && s.IsDeleted == false).ToList();
            foreach (ShowTime st in showTimeList)
            {
                Movie movie = st.MovieSchedule.Movie;
                if (!movieList.Contains(movie) && !movie.IsDeleted)
                    movieList.Add(movie);
            }
            
            return movieList;
        }

        public List<MovieBE> GetAllMoviesBasedOnCinemaToDisplay(int CinemaID)
        {

            List<Movie> dalmovielist = GetAllDALMoviesBasedOnCinemaToDisplay(CinemaID);
            List<MovieBE> movielist = new List<MovieBE>();

            foreach (Movie dalmovie in dalmovielist)
            {
                MovieBE moviebe = ConvertDALToBusinessEntity(dalmovie);
                movielist.Add(moviebe);
            }

            return movielist;

        }

    }
}
